package es.masterd.e11_1;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class GrabadoraActivity extends Activity implements
		OnCheckedChangeListener {
	private MediaRecorder grabadora;

	/** Called when the activity is first created. */
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		ToggleButton rec = (ToggleButton) findViewById(R.id.rec);
		rec.setOnCheckedChangeListener(this);

	}

	
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		try {
			if (isChecked) {
				grabar();
			} else {
				pararGrabar();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void grabar() throws IllegalStateException, IOException {
		grabadora = new MediaRecorder();

		grabadora.setOutputFile("/sdcard/test.3gp");

		// Audio source
		grabadora.setAudioSource(MediaRecorder.AudioSource.MIC);

		// formato de salida
		grabadora.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		grabadora.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

		grabadora.prepare();
		grabadora.start();
	}
	
	protected void pararGrabar() {
		grabadora.stop();
		grabadora.reset();
		grabadora.release();
	}
	
}